package com.hardy.newSolutionAlgorithm;

/**
 * Author: fuxh3@lenovo.com
 * Date: Created in 2017/12/28 0028
 * 众数算法
 * 从候选人中选择
 */
public class Majority {
    public static void main(String[] args) {
        String[] candidateArr = {"A","B","C","B","B","C","A","B","A","B","B","D","B"};
        int c=0;
        String x = "";
        for (int i=0;i<candidateArr.length;i++){
            if(c==0){
                x=candidateArr[i];
            }
            if(candidateArr[i]==x){
                c=c+1;
            }else{
                c=c-1;
            }
        }
        c=0;
        for (int i=0;i<candidateArr.length;i++){
            if(candidateArr[i]==x){
                c=c+1;
            }
        }
        if(c>0.5*candidateArr.length){
            System.out.println(x);
        }else {
            System.out.println("failed");
        }
    }

}
